package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils
import com.lv.common.imp.OnPositionTListener
import com.lv.common.widget.recyclerview.decoration.SpacesItemDecoration
import com.lv.service.R
import com.lv.service.adapter.CardStyleSelectedAdapter
import com.lv.service.bean.CardItemBean
import com.lv.service.room.card.CardColorBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class CardStylePopupView(
    private val mContext: Context,
    private var mPosition: Int,
    private val mStyleBean: MutableList<CardColorBean>,
    private val mCardBean: MutableList<CardItemBean>,
    private val mPositionTListener: OnPositionTListener<List<CardItemBean>>? = null
) :
    BottomPopupView(mContext) {

    private var tvContent: TextView? = null
    private var viewTop: View? = null
    private var iv_close: ImageView? = null
    private var rv_content: RecyclerView? = null
    private var mAdapter: CardStyleSelectedAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.card_style_popup
    }

    override fun onCreate() {
        super.onCreate()
        tvContent = findViewById<TextView>(R.id.tv_content)
        viewTop = findViewById<View>(R.id.view_top)
        iv_close = findViewById<ImageView>(R.id.iv_close)
        rv_content = findViewById<RecyclerView>(R.id.rv_content)

        initView()
        initEvnet()
    }

    private fun initView() {
        mAdapter = CardStyleSelectedAdapter(mStyleBean, mCardBean)
        rv_content?.adapter = mAdapter
        rv_content?.addItemDecoration(SpacesItemDecoration(0, SizeUtils.dp2px(10.0f), false))
        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            if (mPosition == position) {
                mPositionTListener?.onClick(mPosition, mCardBean)
                dismiss()
                return@OnPositionTListener
            }
            if (mPosition != -1) {
                mCardBean[mPosition].isSelectd = false
                mAdapter?.notifyItemChanged(mPosition, R.id.iv_selected)
            }
            mPosition = position
            mCardBean[mPosition].isSelectd = true
            mAdapter?.notifyItemChanged(mPosition, R.id.iv_selected)
            mPositionTListener?.onClick(mPosition, mCardBean)
            dismiss()
        }

    }

    private fun initEvnet() {


        viewTop?.setOnClickListener {
//            mPositionTListener?.onClick(mPosition, mCardBean)
            dismiss()
        }

        iv_close?.setOnClickListener {
//            mPositionTListener?.onClick(mPosition, mCardBean)
            dismiss()
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}